//
//  ZWHomeDetail.m
//  WanEr
//
//  Created by 高文萍 on 15/12/17.
//  Copyright © 2015年 高文萍. All rights reserved.
//

#import "ZWHomeDetail.h"
#import "WanErHeader.h"
#import "ZWCutyDetailCVCell.h"
#import "ZWCityDetailCollectionRV.h"
#import "ZWMainPlayerVC.h"
#import "ZWHomeModel.h"
#import "ZWLunBoView.h"
#import "ZWLuoBoModel.h"
#import "ZWMainPlayerVC.h"
#import "playerWebVC.h"
#import <AVFoundation/AVFoundation.h>
#import "ZWPlayerVC.h"
#import "ZWLeaveDownVC.h"
#import "ZWChouTiView.h"
#import "ZWLikeVC.h"
#import "ZWHomeDetail.m"
#import "ZWBaseTableViewCell.h"
#import "ZWDetailView.h"
#define LBWIDTH [[UIScreen mainScreen] bounds].size.width / 375.0
#define LBHEIGHT [[UIScreen mainScreen] bounds].size.height / 667.0


@interface ZWHomeDetail ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIImageView *cityImgView;
@property (nonatomic, strong) UITableView *cityTable;
//@property (nonatomic, strong) UICollectionViewFlowLayout *cityDtailFL;
@property (nonatomic, strong) NSMutableArray *cityArr;
@property (nonatomic, strong) NSMutableArray *headerArr;;
@property (nonatomic, strong) ZWBaseButton *returnButton;
@property (nonatomic, strong) ZWBaseButton *shareButton;
@property (nonatomic, strong) ZWBaseButton *chou_tiButton;
@property (nonatomic, strong) ZWLunBoView *lun_boView;
@property (nonatomic, strong) NSMutableDictionary *collDic;
@property (nonatomic, strong) MBProgressHUD *homeHud;
@property (nonatomic, strong) ZWMainPlayerVC *playerVC;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) ZWChouTiView *chou_Ti_View;
@property (nonatomic, strong) UIView *choutibackView;
@property (nonatomic, strong) ZWBaseTableViewCell *cell;
@property (nonatomic, strong) ZWDetailView *headerView;

@end

@implementation ZWHomeDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collDic = [NSMutableDictionary dictionary];

    // 通知中心将值传到下一页
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushViedoPage:) name:@"tagValue" object:nil];
    NSLog(@"id---%@", self.uId);
  
    // 刷新
    self.cityTable.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self createData];
        
    }];

    
    [self createTable];
    [self createButton];
    [self createData];
}

#pragma ---创建按钮----
- (void)createButton
{
    self.returnButton = [ZWBaseButton baseButtonFrame:CGRectMake(20, 40, 15, 20) buttonType:UIButtonTypeCustom buttonTitle:nil buttonTitleColor:nil buttonTag:100 buttonImage:[UIImage imageNamed:@"返回"]];
    [self.returnButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.returnButton];
    
    self.chou_tiButton = [ZWBaseButton baseButtonFrame:CGRectMake(WIDTH - 20 - 40, 40, 40, 20) buttonType:UIButtonTypeCustom buttonTitle:nil buttonTitleColor:nil buttonTag:101 buttonImage:[UIImage imageNamed:@"功能"]];
    [self.chou_tiButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.chou_tiButton];
    
    self.shareButton = [ZWBaseButton baseButtonFrame:CGRectMake(WIDTH - 20 - 40 - 20 - 30, 40, 20, 20) buttonType:UIButtonTypeCustom buttonTitle:nil buttonTitleColor:nil buttonTag:102 buttonImage:[UIImage imageNamed:@"分享"]];
    [self.returnButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.shareButton];
}

- (void)click:(UIButton *)button
{
    if (button.tag == 100) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }else if (button.tag == 101){
        NSLog(@"抽屉");
        
        [self createChouTiView];
        [UIView animateWithDuration:0.5 animations:^{
            
            self.chou_Ti_View.frame = CGRectMake(WIDTH / 4, 0, WIDTH / 4 * 3, HEIGHT);
            //        self.homeTableView.frame = CGRectMake(-WIDTH / 4 * 3, 0, WIDTH, HEIGHT);
            self.choutibackView.alpha = 0.5;
            
        }];
        
        
    }else if (button.tag == 102){
        NSLog(@"分享");
    }
}

#pragma --------抽屉View创建------
- (void)createChouTiView
{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    self.choutibackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.choutibackView.backgroundColor = [UIColor darkGrayColor];
    self.choutibackView.alpha = 0;
    [window addSubview:self.choutibackView];
    // 手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    [self.choutibackView addGestureRecognizer:tap];
    
    self.chou_Ti_View = (ZWChouTiView *)[[[NSBundle mainBundle]loadNibNamed:@"ChouTi" owner:self options:nil]lastObject];
    self.chou_Ti_View.frame = CGRectMake(WIDTH , 0, WIDTH / 4 * 3, HEIGHT);
    self.chou_Ti_View.backgroundColor = [UIColor whiteColor];
    [window addSubview:self.chou_Ti_View];
    
    // 滑动手势
    UIPanGestureRecognizer *tapV = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwoClick:)];
    [self.chou_Ti_View addGestureRecognizer:tapV];
    
    // 抽屉View按钮设置
    self.chou_Ti_View.changImgButton.layer.borderWidth = 1;
    self.chou_Ti_View.changImgButton.layer.cornerRadius = 45;
    self.chou_Ti_View.changImgButton.layer.borderColor = [UIColor whiteColor].CGColor;
    self.chou_Ti_View.layer.masksToBounds = YES;
    [self.chou_Ti_View.changImgButton addTarget:self action:@selector(Chou_Ti_Click:) forControlEvents:UIControlEventTouchUpInside];
    self.chou_Ti_View.changImgButton.tag = 1000;
    
    [self.chou_Ti_View.playerButton addTarget:self action:@selector(Chou_Ti_Click:) forControlEvents:UIControlEventTouchUpInside];
    self.chou_Ti_View.playerButton.tag = 1001;
    
    [self.chou_Ti_View.likeButton addTarget:self action:@selector(Chou_Ti_Click:) forControlEvents:UIControlEventTouchUpInside];
    self.chou_Ti_View.likeButton.tag = 1002;
    
    
    [self.chou_Ti_View.linedownButton addTarget:self action:@selector(Chou_Ti_Click:) forControlEvents:UIControlEventTouchUpInside];
    self.chou_Ti_View.linedownButton.tag = 1003;
    
    [self.chou_Ti_View.leaveButton addTarget:self action:@selector(Chou_Ti_Click:) forControlEvents:UIControlEventTouchUpInside];
    self.chou_Ti_View.leaveButton.tag = 1004;
    
}

- (void)tapTwoClick:(UIPanGestureRecognizer *)tap
{
    
    UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)tap;
    CGPoint point = [gesture translationInView:self.chou_Ti_View];
    CGPoint tablePoint = [gesture translationInView:self.cityTable];
    NSLog(@"%f --- %f   --- %f",point.x,point.y,self.chou_Ti_View.frame.origin.x);
    if (self.chou_Ti_View.frame.origin.x + point.x< WIDTH / 4) {
        
        return ;
        
    }else{
        
        self.chou_Ti_View.transform = CGAffineTransformTranslate(self.chou_Ti_View.transform, point.x, self.chou_Ti_View.frame.origin.y);
        
        if (tap.state == UIGestureRecognizerStateRecognized) {
            NSLog(@"结束");
            
            //            self.homeTableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
            if (self.chou_Ti_View.frame.origin.x + point.x > 220) {
                
                [self setViewHidden:YES view:self.chou_Ti_View];
                
            }else{
                
                [self setViewHidden:NO view:self.chou_Ti_View];
            }
            
        }
        
    }
    
    
    [tap setTranslation:CGPointZero inView:self.chou_Ti_View];
    
}

#pragma mark 抽屉弹出消失时调用的方法
- (void)setViewHidden:(BOOL)hidden view:(UIView *)view{
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.15];
    if (hidden) {
        [view setFrame:CGRectMake(WIDTH, 0, WIDTH / 4 * 3 , HEIGHT)];
        
        // firstView 消失时 透明度变为零
        if (view == self.chou_Ti_View){
            self.choutibackView.alpha = 0;
            [self.choutibackView removeFromSuperview];
        }
    }else{
        
        [view setFrame:CGRectMake(WIDTH / 4, 0,WIDTH / 4 * 3, HEIGHT)];
        self.choutibackView.alpha = 0.5;
    }
    [UIView commitAnimations];
}



#pragma -------抽屉上点击方法---------
- (void)Chou_Ti_Click:(UIButton *)button
{
    if (button.tag == 1000) {
        NSLog(@"更换头像");
        
    }else if (button.tag == 1001){
        NSLog(@"正在播放");
        ZWPlayerVC *playerVC = [[ZWPlayerVC alloc]init];
        [UIView animateWithDuration:0.3 animations:^{
            
            self.chou_Ti_View.frame = CGRectMake(WIDTH, 0, WIDTH / 4 * 3, HEIGHT);
            self.choutibackView.alpha = 0;
            
        }];
        [self.navigationController pushViewController:playerVC animated:NO];
        
        
    }else if (button.tag == 1002){
        NSLog(@"喜欢");
        ZWLikeVC *likeVC = [[ZWLikeVC alloc]init];
        [self.navigationController pushViewController:likeVC animated:NO];
        [UIView animateWithDuration:0.3 animations:^{
            
            self.chou_Ti_View.frame = CGRectMake(WIDTH, 0, WIDTH / 4 * 3, HEIGHT);
            self.choutibackView.alpha = 0;

            
        }];
        
        
    }else if (button.tag == 1003){
        NSLog(@"离线");
        ZWLeaveDownVC *leaveDown = [[ZWLeaveDownVC alloc]init];
        leaveDown.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:leaveDown animated:YES completion:nil];
        [UIView animateWithDuration:0.3 animations:^{
            
            self.chou_Ti_View.frame = CGRectMake(WIDTH, 0, WIDTH / 4 * 3, HEIGHT);
            self.choutibackView.alpha = 0;
            
        }];
        
        
    }else if (button.tag == 1004){
        NSLog(@"退出登录");
        
    }
    
}

#pragma ------抽屉收起手势---------
- (void)tapClick:(UITapGestureRecognizer *)tap
{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.chou_Ti_View.frame = CGRectMake(WIDTH, 0, WIDTH / 4 * 3, HEIGHT);
        self.choutibackView.alpha = 0;
        [self.choutibackView removeFromSuperview];

        
    }];
    
    
}

#pragma --------创建网络数据--------
- (void)createData
{
    self.homeHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.homeHud.labelText = @"伟东奔跑中...";
    self.homeHud.alpha = 0.5;
    self.homeHud.labelFont = [UIFont systemFontOfSize:14];
    self.homeHud.labelColor = [UIColor colorWithRed:0.554 green:0.406 blue:0.160 alpha:1.000];
    
    NSString *url = [NSString stringWithFormat:@"http://app.ziwoxz.com/agi/scenerie/info/sid/%@",self.uId];
    [JXRAFNetWorkTool getNetWithURL:url body:nil resultKind:JXRJSON headerFile:nil success:^(id result) {
        
        self.cityArr = [NSMutableArray array];
        self.cityArr =  [ZWHomeModel baseModelByArr:result[@"data"][@"sceneries"] arr:self.cityArr];
        self.cell.arr = self.cityArr;
        self.collDic = result[@"data"][@"main_scenerie"];
        [self.cityImgView sd_setImageWithURL:[NSURL URLWithString:self.collDic[@"image_url"]]];
        self.headerView.cityLabel.text = self.collDic[@"scenerie_name"];
        self.headerView.bigCity.text = self.collDic[@"area"];
        
        [self.cityTable.header endRefreshing];
        
        
        [self.homeHud setHidden:YES];
        [self.homeHud removeFromSuperview];

        [self.cityTable reloadData];
        
    } failure:^(NSError *error) {
        
    }];
 
}

#pragma ----下载全部----
- (void)downLoad:(UIButton *)button
{
    NSLog(@"下载全部");
}

#pragma ---创建tableview---
- (void)createTable
{
    self.cityTable =  [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.cityTable.delegate = self;
    self.cityTable.dataSource = self;
    self.cityTable.contentInset = UIEdgeInsetsMake(252 * LBHEIGHT, 0, 0, 0);
    [self.view addSubview:self.cityTable];
    self.cityTable.rowHeight = 120;
    self.cityTable.tag = 1000;
    self.cityTable.showsVerticalScrollIndicator = NO;
    self.cityTable.showsHorizontalScrollIndicator = NO;
    
    self.cityImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, - 252 * LBHEIGHT, WIDTH, 252 * LBHEIGHT)];
    [self.cityTable addSubview:self.cityImgView];
    
    self.headerView = (ZWDetailView *)[[[NSBundle mainBundle]loadNibNamed:@"Detail" owner:self options:nil]lastObject];
    self.headerView.frame = CGRectMake(0, 0, WIDTH, 130);
    // 字数长的话省略中间
    self.headerView.bigCity.lineBreakMode = NSLineBreakByTruncatingMiddle;
    self.cityTable.tableHeaderView = self.headerView;
}
#pragma ---tableview协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    self.cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!self.cell) {
        self.cell = [[ZWBaseTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    
    [self.cell createData];
    return self.cell;
}

#pragma -- 返回section中cell的高度 --
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500;
}

#pragma --动画--
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
}

- (void)pushViedoPage:(NSNotification *)notification
{
    playerWebVC *pageVC = [playerWebVC new];
    pageVC.uId = notification.userInfo[@"id"];
    
    [self.navigationController pushViewController:pageVC animated:YES];
}

#pragma --- 拖拽时图片也动 ---
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    if (y < - 252 * LBHEIGHT) {
        
        self.cityImgView.frame = CGRectMake((252 * LBHEIGHT + y) / 2, y, WIDTH - (252 * LBHEIGHT + y),  - y);
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
